const app = getApp();
const util = require("../../utils/utils.js")
var inputVal = '';
var msgList = [];
var windowWidth = wx.getSystemInfoSync().windowWidth;
var windowHeight = wx.getSystemInfoSync().windowHeight;
var keyHeight = 0;
/**
 * 初始化数据
 */
function initData(that) {
  inputVal = '';
  msgList = []
  // msgList = [{
  //     speaker: 'server',
  //     content: '欢迎来到英雄联盟，敌军还有30秒到达战场，请做好准备！'
  //   },
  //   {
  //     speaker: 'customer',
  //     content: '我怕是走错片场了了了了了了了了了了了了了了...'
  //   },
  //   {
  //     speaker: 'server',
  //     content: '不，你没走错哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈'
  //   }
  // ]
  var chat
  wx.request({
    url: 'http://localhost:8888/chatting/getChatting',
    method: 'GET',
    data: {
      user1: app.globalData.loginAccount,
      user2: app.globalData.chatWho
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      chat = res.data.data
      var flag = app.globalData.loginAccount == chat.user1
      if(chat.time != null & chat.time != "") {
        chat.time = util.strToArr(chat.time)
      }
      else {
        chat.time = []
      }
      if(chat.content != null & chat.content != "") {
        chat.content = util.strToArr(chat.content)
      }
      else {
        chat.content = []
      }
      if(chat.who != null & chat.who != "") {
        chat.who = util.strToArr(chat.who)
      }
      else {
        chat.who = []
      }
      for (let i = 0; i < chat.content.length; i++) {
        if (flag) {
          if (chat.who[i] == 0) {
            var o = {
              speaker: 'customer',
              content: chat.content[i]
            }
            msgList.push(o)
          } else {
            var o = {
              speaker: 'server',
              content: chat.content[i]
            }
            msgList.push(o)
          }
        } else {
          if (chat.who[i] == 0) {
            var o = {
              speaker: 'server',
              content: chat.content[i]
            }
            msgList.push(o)
          } else {
            var o = {
              speaker: 'customer',
              content: chat.content[i]
            }
            msgList.push(o)
          }
        }
      }
      //console.log(msgList);
      that.setData({
        msgList,
        inputVal
      })
    }
  })

}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hisPhoto: null,
    myPhoto: null,
    scrollHeight: '100vh',
    inputBottom: 0,
  },

  setNickName() {
    var that = this
    wx.request({
      url: 'http://localhost:8888/account/getAccount',
      method: 'GET',
      data: {
        id: app.globalData.chatWho
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          hisPhoto: res.data.data.headimg
        })
        wx.setNavigationBarTitle({
          title: res.data.data.username
        });
      }
    })
  },
  setMyPhoto() {
    var that = this
    wx.request({
      url: 'http://localhost:8888/account/getAccount',
      method: 'GET',
      data: {
        id: app.globalData.loginAccount
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          myPhoto: res.data.data.headimg
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    initData(this);
    this.setNickName();
    this.setMyPhoto();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 获取聚焦
   */
  focus: function (e) {
    keyHeight = e.detail.height;
    this.setData({
      scrollHeight: (windowHeight - keyHeight) + 'px'
    });
    this.setData({
      toView: 'msg-' + (msgList.length - 1),
      inputBottom: keyHeight + 'px'
    })
    //计算msg高度
    // calScrollHeight(this, keyHeight);

  },

  //失去聚焦(软键盘消失)
  blur: function (e) {
    this.setData({
      scrollHeight: '100vh',
      inputBottom: 0
    })
    this.setData({
      toView: 'msg-' + (msgList.length - 1)
    })

  },

  /**
   * 发送点击监听
   */
  sendClick: function (e) {
    var content = e.detail.value.content
    if (content == "") {} else {
      msgList.push({
        speaker: 'customer',
        content: content
      })
      inputVal = '';
      this.setData({
        msgList,
        inputVal
      });
      wx.request({
        url: 'http://localhost:8888/chatting/sendMsg',
        method: 'POST',
        data: {
          user1: app.globalData.loginAccount,
          user2: app.globalData.chatWho,
          content: content
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
        }
      })
    }
  },

  /**
   * 退回上一页
   */
  toBackClick: function () {
    wx.navigateBack({})
  }

})